#!/bin/bash
#
# Copyright 2010 Google Inc.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#      http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

# This script retrieves the value of a given named string from all indicated
# strings.xml files.  If invoked in a project directory (a directory with a
# 'res' subdirectory), and if no strings.xml files are provided, the script
# will automatically analyze res/values*/strings.xml

PROGNAME=$(basename "$0")

function usage() {
  echo "Usage: ${PROGNAME} string_name [file file..]" >&2
  exit 2
}

function die() {
  echo "${PROGNAME}: $@" >&2
  exit 1
}

if [[ "$#" -lt 1 ]] ; then
  usage
fi

name=$1
shift

files=
if [[ $# -eq 0 ]] ; then
  if [[ -d res ]] ; then
    files=res/values*/strings.xml
  else
    die "invoked outside of project root with no file arguments"
  fi
else
  files="$@"
fi

for file in $files ; do
  echo === $file
  xmllint --xpath /resources/string[@name=\"$name\"] $file
  echo
done
